//#include "GeomView.h"
//#include "GeomView.h"
// GeomView.cpp : implementation of the CGeomView class
//

#include "stdafx.h"

#include "GeomDoc.h"
#include "GeomView.h"
#include "AnyView.h"
#include "EditorMgr.h"
#include "GeomApp.h"
#include "ShapeBuilder.h"
#include "VarList.h"
#include "AnyEditor.h"
#include "MessageGeomUI.h"
#include "PropHolder.h"
#include "PropFactory.h"
#include "EditorPropViewer.h"
#include "AnyDocument.h"
#include "SketchPlane.h"
// CGeomView

IMPLEMENT_DYNCREATE(CGeomView, CView)

BEGIN_MESSAGE_MAP(CGeomView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_WM_SIZE()
	ON_WM_MOUSEWHEEL()
	ON_WM_CONTEXTMENU()
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_KEYUP()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_GEOM_STARTEDITOR, OnStartEditor)
	ON_MESSAGE(WM_GEOM_COMMON_MESSAGE, OnReceiveCommand)
	ON_MESSAGE(WM_GEOM_PROPACTION, OnPropAction)
	ON_MESSAGE(WM_GEOM_PLANE, OnSketchPlaneMessage)
END_MESSAGE_MAP()

// CGeomView construction/destruction

CGeomView::CGeomView()
{
	m_pDrawView = new CAnyView();
	m_pEditorMgr = NULL;
}

CGeomView::~CGeomView()
{
	delete m_pDrawView;
	m_pEditorMgr = NULL;
}

BOOL CGeomView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CGeomView drawing

void CGeomView::OnDraw(CDC* /*pDC*/)
{
	CGeomDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	m_pDrawView->Redraw();
}


// CGeomView printing

void CGeomView::OnFilePrintPreview()
{
	BCGPPrintPreview (this);
}

BOOL CGeomView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGeomView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGeomView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CGeomView diagnostics

#ifdef _DEBUG
void CGeomView::AssertValid() const
{
	CView::AssertValid();
}

void CGeomView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGeomDoc* CGeomView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGeomDoc)));
	return (CGeomDoc*)m_pDocument;
}
#endif //_DEBUG


// CGeomView message handlers

void CGeomView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	if(!m_pDrawView->Initialize(GetSafeHwnd(), GetDocument()->GetAnyDoc()))
		AfxMessageBox(_T("Init View Failed!"));
	m_pEditorMgr = m_pDrawView->GetEditorMgr();
}

void CGeomView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	m_pDrawView->Resize();
}

void CGeomView::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_pEditorMgr->OnLButtonDown(nFlags, point.x, point.y);

	CView::OnLButtonDown(nFlags, point);
}

void CGeomView::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_pEditorMgr->OnLButtonUp(nFlags, point.x, point.y);

	CView::OnLButtonUp(nFlags, point);
}
void CGeomView::OnRButtonDown(UINT nFlags, CPoint point)
{
	m_pEditorMgr->OnRButtonDown(nFlags, point.x, point.y);

	CView::OnRButtonDown(nFlags, point);
}

void CGeomView::OnRButtonUp(UINT nFlags, CPoint point)
{
	m_pEditorMgr->OnRButtonUp(nFlags, point.x, point.y);

	CView::OnRButtonUp(nFlags, point);
}

void CGeomView::OnMouseMove(UINT nFlags, CPoint point)
{
	m_pEditorMgr->OnMouseMove(nFlags, point.x, point.y);

	CView::OnMouseMove(nFlags, point);
}

BOOL CGeomView::OnMouseWheel(UINT nFlags, short zDelta, CPoint point)
{
	m_pEditorMgr->OnMouseWheel(nFlags, zDelta, point.x, point.y);

	return CView::OnMouseWheel(nFlags, zDelta, point);
}


void CGeomView::OnContextMenu(CWnd* /*pWnd*/, CPoint /*point*/)
{
	// TODO: Add your message handler code here
}

void CGeomView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	m_pEditorMgr->OnKeyDown(nChar, nRepCnt, nFlags);

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CGeomView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	m_pEditorMgr->OnKeyUp(nChar, nRepCnt, nFlags);

	CView::OnKeyUp(nChar, nRepCnt, nFlags);
}


void CGeomView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	m_pEditorMgr->OnChar(nChar, nRepCnt, nFlags);

	CView::OnChar(nChar, nRepCnt, nFlags);
}

LRESULT CGeomView::OnStartEditor(WPARAM wp, LPARAM lp)
{
	CString* pszCmd = (CString*)wp;
	ASSERT(pszCmd);
	if(NULL == pszCmd)
		return 0;

	CEditorData* pData = (CEditorData*)lp;
	if(NULL == pData)
		return 0;

	if(!m_pEditorMgr->StartEditor(*pszCmd, *pData))
		return 0;

	ShowEditorProp();

	return 0;
}

void CGeomView::ShowEditorProp()
{
	CAnyEditor* pEditor = m_pEditorMgr->GetTopEidtor();
	if(NULL == pEditor)
		return;

	CWnd* pMain = AfxGetMainWnd();
	CShapeInfo* pInfo = pEditor->GetShapeInfo();
	if(NULL == pInfo)
	{
		::SendMessage(pMain->GetSafeHwnd(), WM_GEOM_PROPACTION, GPA_CLEAR,0);
		return;
	}


	CPropHolder* pProp = GetPropFactory().CreateProp(pInfo->GetGUID());
	if(pProp)
	{
		CEditorPropViewer* pEditorPropViewer = new CEditorPropViewer(pEditor, pProp);
		pEditor->SetEditorViewer(pEditorPropViewer);
		::SendMessage(pMain->GetSafeHwnd(), WM_GEOM_PROPACTION, GPA_NEW_PROP,LPARAM(pProp));
		pEditorPropViewer->Show();
		//CVarList param;
		//pInfo->Parameters(param);
		//pProp->SetValues(param);


	}
}


LRESULT CGeomView::OnReceiveCommand(WPARAM wp, LPARAM lp)
{
	switch(wp) 
	{
	case GCM_BROWER_DOC:
		GetDocument()->GetAnyDoc()->BrowserTree();
		break;
	case GCM_FILE_EXPORT:
		{
			CString* szGUID = (CString*)lp;
			if(szGUID)
			{
				GetDocument()->OnFileExport(*szGUID, m_pDrawView);
			}
		}
		break;
	case GCM_FILE_IMPORT:
		{
			CString* szGUID = (CString*)lp;
			if(szGUID)
			{
				GetDocument()->OnFileImport(*szGUID, m_pDrawView);
			}
		}
		break;
	default:
		break;
	}

	return 0;
}

LRESULT CGeomView::OnPropAction(WPARAM wp, LPARAM lp)
{
	if(wp == GPA_UIVALUE_CHANGED)
	{
		CAnyEditor* pEditor = m_pEditorMgr->GetTopEidtor();
		if(pEditor)
		{
			pEditor->OnChangeParam(lp);
		}
	}

	else if(wp == GPA_SHOW_SELECTED_PROP)
	{
		ShowEditorProp();
	}
	return 0;
}

LRESULT CGeomView::OnSketchPlaneMessage(WPARAM wp, LPARAM lp)
{
	if(wp == GPM_NEW_PLANE)
	{
		CSketchPlane::STD_SKETCH_PLANE planeType = (CSketchPlane::STD_SKETCH_PLANE)lp;
		m_pDrawView->UpdatePlane(CSketchPlane::Create(m_pDrawView, planeType));
		m_pEditorMgr->SetDefaultEditor();
	}

	return 0;
}
void CGeomView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
	// TODO: Add your specialized code here and/or call the base class
	if(bActivate)
	{
		GetDocument()->OnActiveDocument(true);
		ShowEditorProp();
	}

			
	CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
